<template>
    <el-dialog v-model="visible" title="签约信息" :close-on-click-modal="false">
        <el-form ref="dataFormRef" label-width="220" :label-position="'top'" :model="dataForm" :rules="dataRules">
            <el-form-item label="签约医疗机构" prop="sgnCtrctOrgCd">
                <el-select v-model="dataForm.sgnCtrctOrgCd" filterable placeholder="请选择签约医疗机构名称" style="width: 100%;">
                    <el-option v-for="(item, index) in orgList" :key="index" :label="item.name"
                        :value="item.orgCode"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="签约医生" prop="sgnCtrctDctNm">
                <el-input v-model="dataForm.sgnCtrctDctNm" maxlength="50" placeholder="请输入签约医生"></el-input>
            </el-form-item>

            <el-form-item label="签约日期" prop="sgnCtrctDt">
                <el-date-picker v-model="dataForm.sgnCtrctDt" type="date" placeholder="请选择签约日期" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="签约生效日期" prop="sgnCtrctStrtDt">
                <el-date-picker v-model="dataForm.sgnCtrctStrtDt" type="date" placeholder="请选择签约生效日期" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="签约到期日期" prop="sgnCtrctExprDt">
                <el-date-picker v-model="dataForm.sgnCtrctExprDt" type="date" placeholder="请选择签约到期日期" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="签约状态" prop="sgnCtrctSttsCd">
                <el-switch v-model="dataForm.sgnCtrctSttsCd" active-text="正常" inactive-text="异常" active-value="0"
                    inactive-value="1" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" :loading="btnLoading" @click="subFormHandle">确定</el-button>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { nextTick, reactive, ref } from 'vue';
import timeFormater from 'time-formater'
import { saveSign, useOrgListApi } from '@/api/patient-primary/patient-mana'
import { ElMessage } from 'element-plus';

const visible = ref(false)
const btnLoading = ref(false)
const dataFormRef = ref()
const emit = defineEmits(['close'])
const dataRules = ref({
    sgnCtrctOrgCd: [{ required: true, message: '请选择签约机构', trigger: 'blur' }],
    sgnCtrctDctNm: [{ required: true, message: '请输入签约医生', trigger: 'blur' }],
    sgnCtrctDt: [{ required: true, message: '请选择签约日期', trigger: 'blur' }],
    sgnCtrctStrtDt: [{ required: true, message: '请选择签约生效日期', trigger: 'blur' }],
    sgnCtrctExprDt: [{ required: true, message: '请选择签约到期日期', trigger: 'blur' }]
})

const dataForm = reactive({
    sgnCtrctOrgCd: '',
    sgnCtrctDctNm: '',
    sgnCtrctSttsCd: '0',
    sgnCtrctStrtDt: '',
    sgnCtrctExprDt: '',
})

const orgList = ref([])

const init = async (data, empiId) => {
    visible.value = true
    btnLoading.value = false

    await useOrgListApi().then((res) => {
        orgList.value = res.data
    })

    nextTick(() => {
        if (dataFormRef.value) {
            dataFormRef.value.resetFields()
        }

        dataForm.empi = empiId

        if (data) {
            Object.assign(dataForm, data)
        } else {
            dataForm.id = ''
        }
    })
}

const subFormHandle = () => {
    dataFormRef.value.validate((valid) => {
        if (!valid) {
            return false
        }
        btnLoading.value = true

        if (dataForm.sgnCtrctDt) {
            dataForm.sgnCtrctDt = timeFormater(dataForm.sgnCtrctDt).format('YYYY-MM-DD')
        }
        if (dataForm.sgnCtrctStrtDt) {
            dataForm.sgnCtrctStrtDt = timeFormater(dataForm.sgnCtrctStrtDt).format('YYYY-MM-DD')

        }
        if (dataForm.sgnCtrctExprDt) {
            dataForm.sgnCtrctExprDt = timeFormater(dataForm.sgnCtrctExprDt).format('YYYY-MM-DD')
        }
        dataForm.sgnCtrctOrgNm = orgList.value.filter((item) => item.orgCode == dataForm.sgnCtrctOrgCd)[0].name

        saveSign(dataForm).then(() => {
            ElMessage.success({
                message: '保存成功',
                duration: 500,
                onClose: () => {
                    btnLoading.value = false
                    visible.value = false
                    emit('close')
                }
            })
        }).catch(() => {
            btnLoading.value = false
        })
    })
}

defineExpose({
    init
})
</script>

<style lang="scss" scoped></style>